import random;

class Activity(object):
    def __init__(self, stgm, sAct, aSubjects, nTime, sId, bCopySegment=False):
        self.stgm = stgm;
        self.nTime = nTime; #REMOVE Later (used only for making Rnd Graphs [but can be used as id="endT.sId"])
        self.sActivity = sAct;
        self.aSubjects = aSubjects;
        self.sId = sId;
        self.aChildNodes = [];
        self.nLikelihood = 0;
        self.rootAct = "None";
        self.bCopySegment = bCopySegment;
    
    def makeXmlGraph(self, graph):
        graph.addnode(self.rootAct, [], self.rootAct.aScores);
        for childNode in self.aChildNodes:
            if int(childNode.aTimes[-1].replace("+",""))!=self.nTime:
                continue;
            aParents = [];
            for parentId in childNode.sParentsId:                
                aParents.append(graph.idndict[parentId]);
            graph.addnode(childNode, aParents, [0.5, childNode.aScores[0]]); #Change 0.5 to Null Prob Model: P(Ch|~M)
    
    def makeXmlNode(self, sId, sActName, aSubjects, aObjects, aObjectTypes, aObjectTimes, aTimes, 
                    sParentsId, aScores, sLocation, bRoot):
        sName = self.getNodeName(sId, sActName, aTimes, aSubjects);
        node = self.stgm.Node(sName, sId=sId, actName=sActName, aSubjects=aSubjects, 
                              timeId=self.nTime, aTimes=aTimes, aScores=aScores, 
                              aObjects=aObjects, aObjectTypes=aObjectTypes, aObjectTimes=aObjectTimes,
                              location=sLocation, sParentsId=sParentsId);
        if bRoot: 
            self.rootAct = node;
            self.nLikelihood = aScores[0];
        else:
            self.aChildNodes.append(node);
    
    def changeInitT(self, nNewInitT):
        self.rootAct.aTimes[0] = str(nNewInitT);
        self.rootAct.name = self.getNodeName(self.sId, self.sActivity, 
                                             self.rootAct.aTimes, self.aSubjects);
#        for childNode in self.aChildNodes:
#            if int(childNode.aTimes[0])<nNewInitT:
#                childNode.aTimes[0] = str(nNewInitT);
#                childNode.name = self.getNodeName(self.sId, self.sActivity, 
#                                                  childNode.aTimes, childNode.aSubjects);
    
    def makeRndGraph(self, graph, nPrior):
        sTime = "t"+str(self.nTime);
        sName = self.getNodeName(self.sId, self.sActivity, [sTime], self.aSubjects);
        sObsId = self.sId+".1";
        obsName = sObsId+"-Obs_t"+str(self.nTime);
        self.nLikelihood = random.random();
        self.rootAct = self.stgm.Node(sName, nId=self.sId, actName=self.sActivity, 
                                      aSubjects=self.aSubjects, aTimes=[sTime], 
                                      aScores=[self.nLikelihood], aObjects=[], location="");
        self.obsNode = self.stgm.Node(obsName, nId=sObsId, actName="Observation", 
                                      aSubjects=self.aSubjects, aTimes=[sTime], 
                                      aScores=[self.nLikelihood], aObjects=[], location="");
        graph.addnode(self.rootAct, [], nPrior);
        graph.addnode(self.obsNode, [self.rootAct], [self.nLikelihood]);
        graph.addobs(self.obsNode, True);
        
    def getNodeName(self, sId, sActName, aTimes, aSubjects):
        sSubjects = "(" + ", ".join(aSubjects) + ")";
        sTimes = "[" + "-".join(aTimes) + "]";
        return sId+"-"+sActName+"\\n"+sSubjects+"\\n"+sTimes;
        
    def getKeyTexts(self):
        return [self.sActivity, self.aSubjects];
            